package com.lzx.adx.report.support.strategy.channel;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Component
@ProductType(ProductChannelConstant.DA_HANG_HAI)
public class DaHangHaiReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String,Object> params=new HashMap<String,Object>();
        params.put("imei", StringUtils.isNotBlank(reportDTO.getImei())?reportDTO.getImei():"");
        params.put("idfa", StringUtils.isNotBlank(reportDTO.getIdfa())?reportDTO.getIdfa():"");
        params.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid())?reportDTO.getOaid():"");
        params.put("imeiMd5", StringUtils.isNotBlank(reportDTO.getImeiSum())?reportDTO.getImeiSum():"");
        params.put("idfaMd5", StringUtils.isNotBlank(reportDTO.getIdfaSum())?reportDTO.getIdfaSum():"");
        params.put("oaidMd5", StringUtils.isNotBlank(reportDTO.getOaidSum())?reportDTO.getOaidSum():"");
        params.put("caid", StringUtils.isNotBlank(reportDTO.getCaid())?reportDTO.getCaid():"");
        params.put("caidMd5", StringUtils.isNotBlank(reportDTO.getCaidSum())?reportDTO.getCaidSum():"");
        params.put("requestId", IdUtil.fastSimpleUUID());
        params.put("ip", reportDTO.getIp());
        if(StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            String Ua = URLEncoderDecoderTool.decodeURL(reportDTO.getUser_agent());
            params.put("ua", URLEncoderDecoderTool.encodeURL(Ua));
        }

        String callbackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());
        String urlParams= UrlUtil.covertParamsOrderByAsc(params)+"&callbackUrl="+ URLEncoderDecoderTool.encodeURL(callbackUrl);


        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }
}
